//
//  XLMineSwitchViewController.m
//  Mine
//
//  Created by 于鹏 on 2017/8/30.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLMineSwitchViewController.h"
#import "XLMineSwitchCellModel.h"
#import "RongIMKit/RongIMKit.h"
@interface XLMineSwitchViewController ()

//@property (nonatomic, assign)BOOL isReceiveNotification;
@property (nonatomic, strong)XLMineSwitchCellModel *receiveNotificationModel;
@end

@implementation XLMineSwitchViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self isReceiveNotification];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    XLWeakSelf(self);
    if (self.type == XLSwitchVCTypeSynch) {
        self.title = @"同步设置";
        XLMineSwitchCellModel *section0 =  [[XLMineSwitchCellModel alloc]initWithTitle:@"同步医疗硬件检查结果" switchType:YES switchBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath, BOOL on) {
            
        }];
        XLMineSwitchCellModel *section1 =  [[XLMineSwitchCellModel alloc]initWithTitle:@"同步会诊聊天记录" switchType:YES switchBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath, BOOL on) {
            
        }];
        XLMineSwitchCellModel *section2 =  [[XLMineSwitchCellModel alloc]initWithTitle:@"同步医疗诊断、用药" switchType:YES switchBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath, BOOL on) {
            
        }];
        [self.xl_dataSource addObject:@[section0]];
        [self.xl_dataSource addObject:@[section1]];
        [self.xl_dataSource addObject:@[section2]];

    } else {
        self.title = @"消息管理";
        self.receiveNotificationModel =  [[XLMineSwitchCellModel alloc]initWithTitle:@"接受新消息通知" switchType:YES switchBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath, BOOL on) {
            [weakself onClickSwitchIsReceiveNotification:on];
        }];
//        self.receiveNotificationModel.on = [self isReceiveNotification];

//        XLMineSwitchCellModel *section1 =  [[XLMineSwitchCellModel alloc]initWithTitle:@"接受语音和视频聊天通知" switchType:YES switchBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath, BOOL on) {
//            
//        }];
        XLMineSwitchCellModel *section2 =  [[XLMineSwitchCellModel alloc]initWithTitle:@"声音" switchType:YES switchBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath, BOOL on) {
            [RCIM sharedRCIM].disableMessageAlertSound = !on;
            UserInfo.disableMessageAlertSound = !on;
        }];
        section2.on = !UserInfo.disableMessageAlertSound;
        
//        XLMineSwitchCellModel *section3 =  [[XLMineSwitchCellModel alloc]initWithTitle:@"震动" switchType:YES switchBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath, BOOL on) {
//            
//        }];
        [self.xl_dataSource addObject:@[self.receiveNotificationModel]];
//        [self.xl_dataSource addObject:@[section1]];
        [self.xl_dataSource addObject:@[section2]];
//        [self.xl_dataSource addObject:@[section3]];

    }
    [self.tableView reloadData];
    self.tableHeaderView = [self defaultTableHeader];
    
}

- (void)initData {
    
}


////判断是否接受消息通知
//- (BOOL)isReceiveNotification {
//   __block BOOL isReceiveNotification;
//    [[RCIMClient sharedRCIMClient]getNotificationQuietHours:^(NSString *startTime, int spansMin) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            if (spansMin >= 1439) {
//                isReceiveNotification = NO;
//            } else {
//                isReceiveNotification = YES;
//            }
//        });
//    } error:^(RCErrorCode status) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            isReceiveNotification = YES;
//        });
//    }];
//    return isReceiveNotification;
//}


//判断是否接受消息通知
- (void)isReceiveNotification {
    __block BOOL isReceiveNotification;
    [[RCIMClient sharedRCIMClient]getNotificationQuietHours:^(NSString *startTime, int spansMin) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (spansMin >= 1439) {
                isReceiveNotification = NO;
                self.receiveNotificationModel.on = isReceiveNotification;
                [self.tableView reloadData];
            } else {
                isReceiveNotification = YES;
                self.receiveNotificationModel.on = isReceiveNotification;
                [self.tableView reloadData];
            }
        });
    } error:^(RCErrorCode status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            isReceiveNotification = YES;
            self.receiveNotificationModel.on = isReceiveNotification;
            [self.tableView reloadData];
        });
    }];
}

//设置是否接收消息
- (void)onClickSwitchIsReceiveNotification:(BOOL)isOn {
    [self showWithStatus:@"设置中"];
    __block BOOL isReceiveNotification;
    if (!isOn) {
        //设置不接收新消息
        [[RCIMClient sharedRCIMClient]setNotificationQuietHours:@"00:00:00" spanMins:1439 success:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self dismissHUD];
                isReceiveNotification = NO;
                self.receiveNotificationModel.on = isReceiveNotification;
                [self.tableView reloadData];
            });
        } error:^(RCErrorCode status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                isReceiveNotification = YES;
                [self showErrorWithStatus:@"设置失败"];
                self.receiveNotificationModel.on = isReceiveNotification;
                [self.tableView reloadData];
            });
        }];
    } else {
        //设置接收新消息
        [[RCIMClient sharedRCIMClient]removeNotificationQuietHours:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self dismissHUD];
                isReceiveNotification = YES;
                self.receiveNotificationModel.on = isReceiveNotification;
                [self.tableView reloadData];
            });
        } error:^(RCErrorCode status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                isReceiveNotification = NO;
                [self showErrorWithStatus:@"设置失败"];
                self.receiveNotificationModel.on = isReceiveNotification;
                [self.tableView reloadData];
            });
        }];
    }
//    self.receiveNotificationModel.on = isReceiveNotification;
//    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
